View Javadoc

1   /**************************************************************************
2   Copyright 2005 Webstersmalley
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  *************************************************************************/
16  
17  
18  package com.webstersmalley.utils.db;
19  
20  import java.sql.Connection;
21  import java.sql.DriverManager;
22  import java.sql.PreparedStatement;
23  import java.sql.SQLException;
24  
25  /***
26   * @author Matthew Smalley
27   */
28  public class Installer
29  {
30  
31      String url;
32      Connection conn;
33      
34      public Installer(String dbPath)
35      {
36          url = "jdbc:derby:" + dbPath + ";create=true";
37      }
38      
39      private void runSQL(String filename, boolean haltOnError) throws Exception
40      {
41          SQLFileParser parser = new SQLFileParser(filename);
42          String statement = parser.nextStatement();
43          while (statement != null)
44          {
45              execute(statement, haltOnError);
46              statement = parser.nextStatement();
47          }
48      }
49      
50      private void execute(String sql, boolean haltOnError) throws Exception
51      {
52          if (sql.endsWith(";"))
53          {
54              sql = sql.substring(0, sql.length()-1);
55          }
56          try
57          {
58              PreparedStatement ps = conn.prepareStatement(sql);
59              ps.executeUpdate();        
60          }
61          catch (SQLException e)
62          {
63              if (haltOnError)
64              {
65                  throw new Exception ("Error running sql '" + sql + "': " + e.toString());
66              }
67          }
68      }
69      
70      public void install()
71      {
72          try
73          {
74              Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
75              conn = DriverManager.getConnection(url);
76              runSQL("src//db//dropobjects.sql", false);
77              runSQL("src//db//createtables.sql", true);
78          }
79          catch (Throwable t)
80          {
81              t.printStackTrace();
82          }
83      }
84      
85      public static void main(String[] args)
86      {
87          if (args.length < 1) 
88          {
89              System.out.println("Usage: Installer path_to_db");
90              System.exit(0);
91          }
92          Installer installer = new Installer(args[0]);
93          installer.install();
94      }
95  }